﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace SF.WebClient.Demos.SingleDemos._11_级联下拉
{
    /// <summary>
    /// Handler2 的摘要说明
    /// </summary>
    public class Handler2 : IHttpHandler
    {
        public static List<dynamic> dataG = new List<dynamic> { 
            new { ID = 1, AreaName = "A1", PID = 1, Sex2 = 23 },
            new { ID = 2, AreaName = "A2", PID = 1, Sex2 = 15 },
            new { ID = 3, AreaName = "A3", PID = 1, Sex2 = 58 },
            new { ID = 4, AreaName = "B1", PID = 2, Sex2 = 4 },
            new { ID = 5, AreaName = "B2", PID = 2, Sex2 = 17 },
            new { ID = 6, AreaName = "C1", PID = 3, Sex2 = 3 },
            new { ID = 7, AreaName = "D1", PID = 3, Sex2 = 35 },
            new { ID = 8, AreaName = "D2", PID = 4, Sex2 = 35 },
            new { ID = 9, AreaName = "E1", PID = 5, Sex2 = 17 },
            new { ID = 10, AreaName = "E2", PID = 5, Sex2 = 17 }
        };

        public void ProcessRequest(HttpContext context)
        {
            object p = context.Request["q"];
            object p2 = context.Request["c1Val"];
            string q = p == null ? "" : p.ToString();
            int f = p2 == null ? 0 : int.Parse(p2.ToString());
            IEnumerable<dynamic> data = new List<dynamic>();

            //根据1级获取2级
            data = dataG.Where(w => w.PID == f);

            if (!string.IsNullOrWhiteSpace(q))
            {
                data = data.Where(w => w.AreaName.Contains(q));
            }

            context.Response.ContentType = "application/json";
            context.Response.Write((new JavaScriptSerializer()).Serialize(data));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}